package com.hhsjdesign.test.act;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class EllipsisTextView extends View {

    /**
     * pointWidth ...的宽度
     * ...
     */
    private TextPaint paint;
    private int textColor = 0xFF000000;
    private int textSize = 40;
    private int pointWidth;
    private int maxWidth = 300;
    private String text = "MMMMMMMMMMMMM";

    public EllipsisTextView(Context context) {
        this(context, null);
    }

    public EllipsisTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EllipsisTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init() {
        paint = new TextPaint();
        paint.setAntiAlias(true);
        paint.setColor(textColor);
        paint.setTextSize(textSize);

        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        paint.setTypeface(font);
//        paint.setFakeBoldText(true); //true为粗体，false为非粗体
//        paint.setTextSkewX(-0.5f);     //float类型参数，负数表示右斜，整数左斜
//        paint.setUnderlineText(true); //true为下划线，false为非下划线
//        paint.setStrikeThruText(true); //true为删除线，false为非删除线
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int textMW = (int) paint.measureText(text);//文字宽度
        int maxW = getMeasuredWidth();//最大宽度
        int width = 0;
        if (textMW > maxW) {
            //表示需要...
            pointWidth = textSize * 8 / 15;
            int leftWidth = maxW - pointWidth;
            int i = text.length();
            while (getTextWidth(text, i) > leftWidth) {
                i--;
            }
            text = text.substring(0, i);
            width = (int) paint.measureText(text);
        }

        canvas.drawText(text, 0, getHeight() / 2 + textSize / 2, paint);

        int pointCenterY = getHeight() / 2 + textSize / 3;
        int pointCenterX = width + textSize * 1 / 9;
        int pointRadius = textSize / 18;

        for (int i = 0; i < 3; i++) {
            canvas.drawCircle(pointCenterX, pointCenterY, pointRadius, paint);
            pointCenterX = pointCenterX + textSize / 5;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = Math.min(maxWidth, width);
        } else {
            width = Math.min(width, (int) paint.measureText(text));
        }

        if (heightMode != MeasureSpec.EXACTLY) {
            height = textSize + (textSize / 5) * 2;
        }

        setMeasuredDimension(width, height);
    }

    private int getTextWidth(String text, int position) {
        return (int) paint.measureText(text.substring(0, position));
    }


    public void setTextSize(int size) {
        this.textSize = size;
        invalidate();
    }


    public void setTextColor(int color) {
        this.textColor = color;
        invalidate();
    }

    public void setText(String text) {
        if (text != null && !text.equals(this.text)) {
            this.text = text;
            invalidate();
        }
    }


    public void setMaxWidth(int widthPx) {
        if (widthPx > 0) {
            this.maxWidth = widthPx;
            invalidate();
        }
    }
}
